"""
-*- coding: utf-8 -*-
@Author  : Link
@Time    : 2022/5/23 15:46
@Software: PyCharm
@File    : stdf_create_script.py
@Remark  : 
"""
from Semi_ATE import STDF


class FakeStdfBase:
    f = None

    def open(self, path: str):
        if self.f is not None:
            raise Exception("Error Open Fail")
        self.f = open(path, mode="wb")

    def save(self):
        if self.f is None:
            raise Exception("Error Open Fail")
        self.f.close()
        self.f = None

    def far(self):
        record = STDF.FAR()
        self.f.write(record.__repr__())

    def atr(self, cmd_line='SAVE AS WITH PYTHON STDF CONVERT'):
        record = STDF.ATR()
        record.set_value('CMD_LINE', cmd_line)
        self.f.write(record.__repr__())

    def mir(self, **kwargs):
        record = STDF.MIR()
        record.set_value("SETUP_T",  kwargs["SETUP_T"])
        record.set_value("START_T",  kwargs["START_T"])
        record.set_value("STAT_NUM", 1)
        record.set_value("MODE_COD", 'P')
        record.set_value("LOT_ID",   kwargs["LOT_ID"])
        record.set_value("PART_TYP", kwargs["PART_TYP"])
        record.set_value("NODE_NAM", kwargs["NODE_NAM"])
        record.set_value("TSTR_TYP", kwargs["TSTR_TYP"])
        record.set_value("JOB_NAM",  kwargs["JOB_NAM"])
        record.set_value("SBLOT_ID", kwargs["SBLOT_ID"])
        record.set_value("FLOW_ID", "WAT")
        self.f.write(record.__repr__())

    def wir(self, **kwargs):
        record = STDF.WIR()
        record.set_value("HEAD_NUM", 1)
        record.set_value("SITE_GRP", 255)
        record.set_value("START_T",  kwargs["START_T"])
        record.set_value("WAFER_ID", kwargs["WAFER_ID"])
        self.f.write(record.__repr__())

    def pir(self, **kwargs):
        record = STDF.PIR()
        record.set_value("HEAD_NUM", 1)
        record.set_value("SITE_NUM", kwargs["SITE_NUM"])
        self.f.write(record.__repr__())

    def ptr(self, **kwargs):
        record = STDF.PTR()
        record.set_value("TEST_NUM", kwargs["TEST_NUM"])
        record.set_value("HEAD_NUM", 1)
        record.set_value("SITE_NUM", kwargs["SITE_NUM"])
        record.set_value("TEST_TXT", kwargs["TEST_TXT"])
        record.set_value("TEST_FLG", kwargs["TEST_FLG"])
        record.set_value("OPT_FLAG", 0)
        record.set_value("PARM_FLG", 0)
        record.set_value('RES_SCAL', 0)
        record.set_value('LLM_SCAL', 0)
        record.set_value('HLM_SCAL', 0)
        record.set_value("LO_LIMIT", float(kwargs["LO_LIMIT"]))
        record.set_value("HI_LIMIT", float(kwargs["HI_LIMIT"]))
        record.set_value("RESULT",   float(kwargs["RESULT"]))
        record.set_value("UNITS", kwargs["UNITS"])
        self.f.write(record.__repr__())

    def prr(self, **kwargs):
        record = STDF.PRR()
        record.set_value("HEAD_NUM", 1)
        record.set_value("SITE_NUM", kwargs["SITE_NUM"])
        record.set_value("PART_FLG", kwargs["PART_FLG"])
        record.set_value("NUM_TEST", kwargs["NUM_TEST"])
        record.set_value("HARD_BIN", kwargs["HARD_BIN"])
        record.set_value("SOFT_BIN", kwargs["SOFT_BIN"])
        record.set_value("X_COORD",  int(kwargs["X_COORD"]))
        record.set_value("Y_COORD",  int(kwargs["Y_COORD"]))
        record.set_value("TEST_T",   kwargs["TEST_T"])
        record.set_value("PART_ID",  str(kwargs["PART_ID"]))
        self.f.write(record.__repr__())

    def mrr(self, **kwargs):
        record = STDF.MRR()
        record.set_value("FINISH_T", kwargs["FINISH_T"])
        self.f.write(record.__repr__())
